/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.SuccessStoryBO;
import entity.SuccessStory;
import java.awt.Color;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class DialogSuccess extends javax.swing.JDialog {

    public static final int SHOW = 0;
    public static final int ADD = 1;
    public static final int EDIT = 2;
    private final Main parent;

    /**
     * Creates new form DialogSuccess
     */
    public DialogSuccess(Main parent, boolean modal, int mode, SuccessStory data) {
        super(parent, modal);
        initComponents();
        InstallEsc.installEscapeCloseOperation(this);
        this.getContentPane().setBackground(Color.decode("#121710"));
        this.parent = parent;
        switch (mode) {
            case 0: {
                btnAdd.setVisible(false);
                btnEdit.setVisible(false);
                btnDelete.setVisible(false);

                setData(data);

                txtStoriesName.setEditable(false);
                txtStoriesSuccess.setEditable(false);
            }
            break;
            case 1: {
                btnEdit.setVisible(false);
                btnDelete.setVisible(false);
                setData(null);
            }
            break;
            case 2: {
                btnAdd.setVisible(false);
                setData(data);
            }
            break;
        }
    }

    private void setData(SuccessStory data) {
        if (data == null) {
            txtId.setText("");
            txtStoriesName.setText("");
            txtStoriesSuccess.setText("");
            txtStoriesSuccess.setLineWrap(true);
        } else {
            txtId.setText(data.getId() + "");
            txtStoriesName.setText(data.getName());
            txtStoriesSuccess.setText(data.getStory().replace("\\n", "\n"));
            txtStoriesSuccess.setLineWrap(true);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlHeader = new javax.swing.JPanel();
        txtId = new javax.swing.JTextField();
        txtStoriesName = new javax.swing.JTextField();
        pnlFooter = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        pnlBody = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtStoriesSuccess = new javax.swing.JTextArea();
        lblStorySuccess = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        pnlHeader.setBackground(new java.awt.Color(90, 111, 72));
        pnlHeader.setToolTipText("");

        txtId.setEditable(false);
        txtId.setBackground(new java.awt.Color(90, 111, 72));
        txtId.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        txtId.setForeground(new java.awt.Color(255, 255, 255));
        txtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtId.setText("jTextField1");
        txtId.setBorder(null);
        txtId.setOpaque(false);

        txtStoriesName.setBackground(new java.awt.Color(90, 111, 72));
        txtStoriesName.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        txtStoriesName.setForeground(new java.awt.Color(255, 255, 255));
        txtStoriesName.setBorder(null);
        txtStoriesName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtStoriesNameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlHeaderLayout = new javax.swing.GroupLayout(pnlHeader);
        pnlHeader.setLayout(pnlHeaderLayout);
        pnlHeaderLayout.setHorizontalGroup(
            pnlHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlHeaderLayout.createSequentialGroup()
                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtStoriesName, javax.swing.GroupLayout.PREFERRED_SIZE, 545, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlHeaderLayout.setVerticalGroup(
            pnlHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlHeaderLayout.createSequentialGroup()
                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(txtStoriesName)
        );

        pnlFooter.setBackground(new java.awt.Color(18, 23, 16));
        pnlFooter.setToolTipText("");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        pnlFooter.add(btnAdd);

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnlFooter.add(btnEdit);

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnlFooter.add(btnDelete);

        pnlBody.setBackground(new java.awt.Color(18, 23, 16));
        pnlBody.setToolTipText("");

        txtStoriesSuccess.setBackground(new java.awt.Color(18, 23, 16));
        txtStoriesSuccess.setColumns(20);
        txtStoriesSuccess.setForeground(new java.awt.Color(255, 255, 255));
        txtStoriesSuccess.setRows(5);
        jScrollPane1.setViewportView(txtStoriesSuccess);

        lblStorySuccess.setBackground(new java.awt.Color(18, 23, 16));
        lblStorySuccess.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblStorySuccess.setForeground(new java.awt.Color(255, 255, 255));
        lblStorySuccess.setText("Story Detail");

        javax.swing.GroupLayout pnlBodyLayout = new javax.swing.GroupLayout(pnlBody);
        pnlBody.setLayout(pnlBodyLayout);
        pnlBodyLayout.setHorizontalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBodyLayout.createSequentialGroup()
                .addComponent(lblStorySuccess, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1))
        );
        pnlBodyLayout.setVerticalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBodyLayout.createSequentialGroup()
                .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblStorySuccess, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlFooter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlBody, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlFooter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowLostFocus

    private void txtStoriesNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtStoriesNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtStoriesNameActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        try {
            // TODO add your handling code here:
            int i = JOptionPane.showConfirmDialog(this, "Do you want to Add this SuccessStory?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) // neu nguoi dung chon "Yes"
            {
            new SuccessStoryBO().addSuccessStory(new SuccessStory(0, txtStoriesName.getText(), txtStoriesSuccess.getText()));
                JOptionPane.showMessageDialog(this, "Add successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            }
            parent.loadData();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: Database Link Error", "Message Dialog", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            int i = JOptionPane.showConfirmDialog(this, "Do you want to Edit this SuccessStory?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) // neu nguoi dung chon "Yes"
            {
            new SuccessStoryBO().editSuccessStory(new SuccessStory(Integer.parseInt(txtId.getText()), txtStoriesName.getText(), txtStoriesSuccess.getText()));
                JOptionPane.showMessageDialog(this, "Edit successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            }
            parent.loadData();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: Database Link Error", "Message Dialog", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        try {
            // TODO add your handling code here:
            
            int i = JOptionPane.showConfirmDialog(this, "Do you want to Delete this SuccessStory?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) // neu nguoi dung chon "Yes"
            {
                new SuccessStoryBO().deleteSuccessStory(Integer.parseInt(txtId.getText()));
                JOptionPane.showMessageDialog(this, "Delete successful", "", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: Database Link Error", "Message Dialog", JOptionPane.ERROR_MESSAGE);
        }
        parent.loadData();
    }//GEN-LAST:event_btnDeleteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogSuccess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogSuccess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogSuccess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogSuccess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblStorySuccess;
    private javax.swing.JPanel pnlBody;
    private javax.swing.JPanel pnlFooter;
    private javax.swing.JPanel pnlHeader;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtStoriesName;
    private javax.swing.JTextArea txtStoriesSuccess;
    // End of variables declaration//GEN-END:variables

    private String getId() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private String getStoriesName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private String getSuccessStories() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
